package per.com.controller.backend;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import per.com.bean.User;
import per.com.common.Const;
import per.com.common.ServerResponse;
import per.com.service.IUserManageService;
import per.com.service.PcUsersService;

import javax.servlet.http.HttpSession;

/**
 * @author zxy
 */
@Controller
@RequestMapping(value = "/backend/user")
public class UserManageController {

    @Autowired
    @Qualifier("PcUsersServiceId")
    private PcUsersService pcUsersService;

    @Autowired
    private IUserManageService iUserManageService;

    /**
     * 管理员用户登录
     * @param username
     * @param password
     * @param session
     * @return
     */
    @RequestMapping(value = "/adminLogin" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> adminLogin(String username , String password , HttpSession session){

        ServerResponse<User> response = pcUsersService.login(username , password);
        if (response.isSuccess()){
            User user = response.getData();
            if (user.getStatus() == Const.Role.ROLE_ADMIN){
                session.setAttribute(Const.CURRENT_USER , user);
            }else {
                return ServerResponse.createByErrorMessage("不是管理员，无法登录");
            }
        }
        return response;
    }

    /**
     * 分页用户列表
     * @param pageNum
     * @param pageSize
     * @param session
     * @return
     */
    @RequestMapping(value = "/list" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<PageInfo> userList(@RequestParam(value = "pageNum" , defaultValue = "1") int pageNum ,
                                             @RequestParam(value = "pageSize" , defaultValue = "10") int pageSize,
                                             HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("用户未登录，请先登录");
        }

        if (iUserManageService.checkAdminRole(user).isSuccess()){
            return iUserManageService.userList(pageNum , pageSize);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }

    }


    /**
     * 修改用户冻结状态
     * @param session
     * @param userId
     * @param isfreeze
     * @return
     */
    @RequestMapping(value = "/isfreeze" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse freeze(HttpSession session , String userId , Integer isfreeze){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("用户未登录，请先登录");
        }

        if (iUserManageService.checkAdminRole(user).isSuccess()){
            return iUserManageService.freeze(userId , isfreeze);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }

    }

    /**
     * 修改用户信誉积分
     * @param session
     * @param userId
     * @param creditScore
     * @return
     */
    @RequestMapping(value = "update_creditscore" , method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse updateCreditScore(HttpSession session , String userId , Integer creditScore){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null){
            return ServerResponse.createByErrorMessage("用户未登录，请先登录");
        }

        if (iUserManageService.checkAdminRole(user).isSuccess()){
            return iUserManageService.updateCreditScore(userId , creditScore);
        }else {
            return ServerResponse.createByErrorMessage("无权限操作");
        }

    }
}
